/*-------------------------------------------------------------------------

***************** Modification History ***********************************
   @doc
   @module Recptfrm.c - Receipt building functions |
   @end

$Log: /SoftPay/SPK2SIM/Source/Print/recptfrm.c $
**************************************************************************

   Copyright (C) 1999 by VeriFone, Inc.

   All rights reserved.  No part of this software may be reproduced,
   transmitted, transcribed, stored in a retrieval system, or translated
   into any language or computer language, in any form or by any means,
   electronic, mechanical, magnetic, optical, chemical, manual or otherwise,
   without the prior written permission of VeriFone, Inc.

---------------------------------------------------------------------------*/
#define VSRETFRM_C

#include <aclconio.h>
#include <stdlib.h>
#include <string.h>
#include <aclstr.h>

#include <message.h>
#include <aclutil.h>
#include <formater.h>
#include <printer.h>
#include <project.h>

/** /#define DEBUG_VSRETFRM*/
#ifdef DEBUG_VSRETFRM
#define DEBUG_95
#endif

#include <define.h>
#include <transact.h>
#include <accum.h>
#include <print.h>
#include <form.h>
#include <cdt.h>
#include <hdt.h>
#include <tblutil.h>
#include <tct.h>
#include <pct.h>
#include <bdt.h>
#include <sdt.h>
#include <trt.h>
#include <msg.h>
#include <mht.h>
#include <mit.h>
#include <iit.h>
#include <amount.h>
#include <msgfuncs.h>
#include <format.h>
#include <form.h>
#include <pform.h>
#include <tran.h>
#include <entry.h>
#include <ui.h>
#include <date.h>
#include <spdebug.h>
#include <isoutl.h>
#include <clerk.h>
#include <oper.h>
#include <card.h>
#include <pbatch.h>
#include <tip.h>

extern char* pchGetMessage(long);
extern char szTempMsg[MESSAGE_SIZE + 1];
char szLongStr[LONG_STR_SIZE + 1] = "%ld";
char szIntStr[INT_STR_SIZE + 1] = "%d";
int trancount;

unsigned long lnHeaderCondition;
unsigned long lnMiddleCondition;
unsigned long lnBottomCondition;

int inSetReceiptTopConditions(TRANSACTION_OBJECT* pobTran) {
    if (pobTran->fPreprinted == VS_TRUE)
        return (VS_SUCCESS);

    if (inGetPrinterType() == NOPRINTER || !fGetEnablePrinter())
        return(VS_SUCCESS);


    lnHeaderCondition = formater_set_flags(((fRefundTrans(pobTran)) || pobTran->srBKey.fVoided), (fGetDemoMode()), (inGetClerkMode() == NO_CLERK && inGetShiftType() != NO_SHIFT), (pobTran->srBRec.inPrintOption & PRT_DUPLICATE), (inGetApplicationType() == RETAIL && inGetClerkMode() != NO_CLERK && inGetShiftType() != NO_SHIFT), (inGetApplicationType() == RESTAURANT && inGetClerkMode() != NO_CLERK && inGetShiftType() != NO_SHIFT), (inGetApplicationType() == RETAIL && inGetClerkMode() != NO_CLERK && inGetShiftType() == NO_SHIFT), (inGetApplicationType() == RESTAURANT && inGetClerkMode() != NO_CLERK && inGetShiftType() == NO_SHIFT), (strlen(srRcpt.szHeaderLine) > 0),
                        (strlen(srRcpt.szHeaderLine2) > 0),
                        (strlen(srRcpt.szHeaderLine3) > 0),
                        (strlen(srRcpt.szHeaderLine4) > 0),

                        (strlen(srRcpt.szHeaderLine5) > 0),
                        (strlen(srRcpt.szHeaderLine6) > 0),
                        0,
                        0,

                        0,
                        0,
                        0,
                        0,


                        0,
                        0,
                        0,
                        0,

                        0,
                        0,
                        0,
                        0,

                        0,
                        0,
                        0,
                        0, -1);

    return (VS_SUCCESS);
}
/*-------------------------------------------------------------------------
   Function :    inSetReceiptMiddleConditions()
    Description :
    Parameters :
    Returns :
    Notes :
--------------------------------------------------------------------------*/

int inSetReceiptMiddleConditions(TRANSACTION_OBJECT* pobTran) {
    VS_BOOL fShowAVS = VS_FALSE;
    VS_BOOL fVisaDebitFields = VS_FALSE;
    VS_BOOL fVisaPurchaseCardFields = VS_FALSE;
    int inAVSMode;
    VS_BOOL fExpDate = VS_TRUE;
    VS_BOOL fCVV2Line = VS_FALSE;

    /** /pdebug(("--inSetReceiptMiddleConditions--"));*/

    if (inGetPrinterType() == NOPRINTER || !fGetEnablePrinter()) {
        //      vdDisplayMessageFromFile( NO_PRINTER_MSG, WARNING_MSG);
        return(VS_SUCCESS);
    }

    if (inGetIssuerRecord(inGetIssuerNum()) == VS_SUCCESS) {
        if (pobTran->srBRec.inPrintOption & PRT_MERCH)
            fExpDate = fGetMerchExpDate();
        else
            fExpDate = fGetCustExpDate();
    }

    if (srRcpt.szAVSCode[0] == NULL_CH)
        inAVSMode = NO_AVS;
    else
        inAVSMode = inGetAVS();

    if (fPhoneTrans(pobTran) || (pobTran->srBRec.inCode == SALE && pobTran->srBRec.fManual == VS_TRUE && fGetCardPresent() == VS_TRUE))
        fShowAVS = VS_TRUE;

    if (srRcpt.unFrm.srReceipt.szCVV2RespCode[0] != NULL_CH && ((inGGCode() == SALE) || (inGGCode() == PHONE)) && (pobTran->srBRec.fManual == VS_TRUE) && (inGetCVV_II() != NO_CVV2) && !(inGetCVV_II() == CVV2_CARD_NOT_PRESENT && fGetCardPresent() && pobTran->fCardPresent) && !pobTran->srBKey.fOffline)
        fCVV2Line = VS_TRUE;

    if ((pobTran->srBRec.inTranCardType == DEBIT_CARD) && strlen(srRcpt.unFrm.srReceipt.szRefCustNumber) > 0)
        fVisaDebitFields = VS_TRUE;

    if ((pobTran->srBRec.inTranCardType == PURCHASE_CARD) && strlen(srRcpt.unFrm.srReceipt.szRefCustNumber) > 0)
        fVisaPurchaseCardFields = VS_TRUE;

    lnMiddleCondition = formater_set_flags((inGGCode() == PRE_AUTH), (fVisaDebitFields), (fVisaPurchaseCardFields), 0, fExpDate, fCVV2Line, 0, 0, (strlen(srRcpt.unFrm.srReceipt.szServId) > 0),
                        0,
                        (fShowAVS && (inAVSMode != NO_AVS)),
                        (fGetPrintBatNum()),

                        0,
                        0,
                        0,
                        0,

                        0,
                        0,
                        0,
                        0,

                        0,
                        0,
                        0,
                        0,

                        0,
                        0,
                        0,
                        0,

                        0,
                        0,
                        0,
                        0, -1);
    return (VS_SUCCESS);
}




int inSetReceiptBottomConditions(TRANSACTION_OBJECT* pobTran) {
    VS_BOOL fTipBlankLine = VS_FALSE;
    VS_BOOL fTipLine = VS_FALSE;
    VS_BOOL fTaxBlankLine = VS_FALSE;
    VS_BOOL fNoSigLineMsg = VS_FALSE;
    VS_BOOL fDisclaimerLine = VS_FALSE;
    VS_BOOL fTotalLineOnly = VS_FALSE;
    VS_BOOL fCustomerCopy = VS_FALSE;
    VS_BOOL fGratuityLines = VS_FALSE;
    int inTranCode;

    /** /pdebug(("--vdSetPrintReceiptConditions--"));*/

    if (inGetPrinterType() == NOPRINTER || !fGetEnablePrinter()) {
        //      vdDisplayMessageFromFile( NO_PRINTER_MSG, WARNING_MSG);
        return(VS_SUCCESS);
    }

    inTranCode = inGGCode();

    if ((inTranCode == PRE_AUTH || inTranCode == PRE_COMP || inTranCode == SALE || inTranCode == PHONE || inTranCode == HH_SALE) && (pobTran->srBRec.inTranCardType != DEBIT_CARD && pobTran->srBRec.inTranCardType != EBT_CARD) && (inGetTipProcessing() > TIP_OFF) && (!fRefundTrans(pobTran)) && (!pobTran->srBKey.fVoided) && (pobTran->srBRec.lnTipAmount == 0L))
        fTipBlankLine = VS_TRUE;

    if ((!fTipBlankLine) && ((fRefundTrans(pobTran) && pobTran->srBRec.inTranCardType != EBT_CARD) || (inTranCode == SALE_OFFLINE)) && (inGetTipProcessing() > TIP_OFF) && (pobTran->srBRec.lnTipAmount == 0L))
        fTipLine = VS_TRUE;

    if (pobTran->srBRec.fTipEntered && !fTipBlankLine)
        fTipLine = VS_TRUE;

    if ((inTranCode == PRE_AUTH) && (pobTran->srBRec.inTranCardType != DEBIT_CARD && pobTran->srBRec.inTranCardType != EBT_CARD) && (fGetTaxProc() == VS_TRUE) && (pobTran->srBRec.lnTaxAmount == 0L))
        fTaxBlankLine = VS_TRUE;

    if (pobTran->srBRec.inPrintOption & PRT_CUST) {
        fNoSigLineMsg = VS_TRUE;
        fCustomerCopy = VS_TRUE;
    } else if (inTranCode == PRE_AUTH) {
        if (!fGetOpenTabSLine() && (pobTran->srBRec.inPrintOption & PRT_MERCH))
            fNoSigLineMsg = VS_TRUE;
    } else if (!fGetSignLine() && (pobTran->srBRec.inPrintOption & PRT_MERCH))
        fNoSigLineMsg = VS_TRUE;


    if (fGetPrintDisclaimer() && (pobTran->srBRec.inPrintOption & PRT_MERCH))
        fDisclaimerLine = VS_TRUE;


    /* print signature line for floor limit transaction */

    if ((fFPSQualified(pobTran) == VS_TRUE) && (inGetFPSType() != FPS_NO) && (inGetFPSType() != FPS_FLOOR))
        fNoSigLineMsg = VS_TRUE;

    /* Make sure if No Signature Required, Disclaimer line will not print */

    if (fNoSigLineMsg)
        fDisclaimerLine = VS_FALSE;

    if (pobTran->srBRec.inTranCardType == PURCHASE_CARD)
        fTotalLineOnly = VS_FALSE;
    else if ((((fGetTaxProc() != VS_TRUE) || ((fGetTaxProc() == VS_TRUE) && pobTran->fHHNoTax == VS_TRUE)) && (inGetTipProcessing() == TIP_OFF) && (!pobTran->srBRec.lnSChgAmount) && (!pobTran->srBRec.lnCashBackAmount)))
        fTotalLineOnly = VS_TRUE;

    if (inTranCode == AUTH_ONLY || inTranCode == IMAM_CARDVER)
        fTotalLineOnly = VS_TRUE;

    if (pobTran->srBRec.inTranCardType == EBT_CARD && (!pobTran->srBRec.lnSChgAmount) && (!pobTran->srBRec.lnCashBackAmount))
        fTotalLineOnly = VS_TRUE;

    if ((fGetSugTipOn()) && (inGetTipProcessing() > TIP_OFF) && (fTipBlankLine) && (!fCustomerCopy) && (inTranCode != PRE_AUTH))
        fGratuityLines = VS_TRUE;

    lnBottomCondition = formater_set_flags((fDisclaimerLine), (inTranCode == REFUND_DEBIT), 0, /* Do not use, reserved for error receipt */
                        (pobTran->srBRec.inPrintOption & PRT_CUST), (strlen(srRcpt.szSpecialMessage) > 0),
                        (strlen(srRcpt.szStatusMessage) > 0),
                        (strlen(srRcpt.szStatus2Message) > 0),
                        (strlen(srRcpt.szErrorMessage) > 0),

                        ((fNoSigLineMsg) && (!fCustomerCopy)),
                        ((inTranCode != PRE_AUTH) && !fTotalLineOnly),
                        (!fNoSigLineMsg),
                        (!pobTran->srBKey.fVoided && !fRefundTrans(pobTran) && !fTotalLineOnly),

                        (pobTran->srBRec.lnCashBackAmount > 0) ,
                        (pobTran->srBRec.lnSChgAmount > 0L),
                        (fGratuityLines),
                        (!fTipBlankLine && !fTaxBlankLine && inTranCode != PRE_AUTH),

                        (pobTran->srBRec.fTaxEntered && !fTaxBlankLine),
                        (fTaxBlankLine),
                        (fTipLine),
                        (fTipBlankLine),

                        (strlen(srRcpt.szMultiAppMsg1) > 0),
                        (strlen(srRcpt.szMultiAppMsg2) > 0),
                        0,
                        0,

                        0,
                        0,
                        0,
                        0,

                        0,
                        0,
                        0,
                        0, -1);

    return (VS_SUCCESS);
}

int inMakeReceiptAmount(TRANSACTION_OBJECT* pobTran) {
    long lnSChg = 0L;
    long lnSubTotAmt;

    memset(srRcpt.unFrm.srReceipt.szBaseAmt, 0, sizeof(srRcpt.unFrm.srReceipt.szBaseAmt));
    memset(srRcpt.unFrm.srReceipt.szCBack, 0, sizeof(srRcpt.unFrm.srReceipt.szCBack));
    memset(srRcpt.unFrm.srReceipt.szTipAmt, 0, sizeof(srRcpt.unFrm.srReceipt.szTipAmt));
    memset(srRcpt.unFrm.srReceipt.szTaxAmt, 0, sizeof(srRcpt.unFrm.srReceipt.szTaxAmt));
    memset(srRcpt.unFrm.srReceipt.szTotAmt, 0, sizeof(srRcpt.unFrm.srReceipt.szTotAmt));
    memset(srRcpt.unFrm.srReceipt.szSubTotAmt, 0, sizeof(srRcpt.unFrm.srReceipt.szSubTotAmt));
    memset(srRcpt.szTipAmt1, 0, sizeof(srRcpt.szTipAmt1));
    memset(srRcpt.szTipAmt2, 0, sizeof(srRcpt.szTipAmt2));
    memset(srRcpt.szTipAmt3, 0, sizeof(srRcpt.szTipAmt3));


    if ((fGetCashBack()) && (pobTran->srBRec.lnCashBackAmount > 0L)) {
        sprintf(srRcpt.unFrm.srReceipt.szCBack, SUBSTITUTE_LD, pobTran->srBRec.lnCashBackAmount);
        vdFormatAmount(srRcpt.unFrm.srReceipt.szCBack, VS_TRUE);
    }

    //    if (pobTran->srBRec.fTipEntered)
    if (inGetTipProcessing() > TIP_OFF) {
        sprintf(srRcpt.unFrm.srReceipt.szTipAmt, SUBSTITUTE_LD, pobTran->srBRec.lnTipAmount);
        vdFormatAmount(srRcpt.unFrm.srReceipt.szTipAmt, VS_TRUE);
    }

    if (pobTran->srBRec.fTaxEntered) {
        sprintf(srRcpt.unFrm.srReceipt.szTaxAmt, SUBSTITUTE_LD, pobTran->srBRec.lnTaxAmount);
        vdFormatAmount(srRcpt.unFrm.srReceipt.szTaxAmt, VS_TRUE);
    }

    if (pobTran->srBRec.lnSChgAmount > 0L) {
        lnSChg = pobTran->srBRec.lnSChgAmount;

        memset(srRcpt.unFrm.srReceipt.szSChg, 0, sizeof(srRcpt.unFrm.srReceipt.szSChg));
        sprintf(srRcpt.unFrm.srReceipt.szSChg, SUBSTITUTE_LD, lnSChg);
        vdFormatAmount(srRcpt.unFrm.srReceipt.szSChg, VS_TRUE);

        lnSubTotAmt = (pobTran->srBKey.lnTotalOfTransactionAmount - lnSChg - pobTran->srBRec.lnCashBackAmount);
        sprintf(srRcpt.unFrm.srReceipt.szSubTotAmt, SUBSTITUTE_LD, lnSubTotAmt);
        vdFormatAmount(srRcpt.unFrm.srReceipt.szSubTotAmt, VS_TRUE);
    }

    sprintf(srRcpt.unFrm.srReceipt.szTotAmt, SUBSTITUTE_LD, (pobTran->srBKey.lnTotalOfTransactionAmount));
    vdFormatAmount(srRcpt.unFrm.srReceipt.szTotAmt, VS_TRUE);

    return(VS_SUCCESS);
}
/*-------------------------------------------------------------------------
    Function :    inMakeReceiptTop
    Description :
    Parameters :
    Returns :
    Notes :
--------------------------------------------------------------------------*/

int inMakeReceiptTop(TRANSACTION_OBJECT* pobTran) {
    char szExpDate[EXP_DATE_SIZE + 1];
    char room[ROOM_NUM_SIZE + 1];
    char szTransName[MESSAGE_SIZE + 1];

    if (inGetPrinterType() == NOPRINTER || !fGetEnablePrinter())
        return(VS_SUCCESS);

    if (pobTran->fPreprinted == VS_TRUE)
        return (VS_SUCCESS);
    if (fGetDemoMode() == VS_FALSE)
        trancount = 0;

    memset(srRcpt.szHeaderLine, 0, (sizeof(srRcpt.szHeaderLine) * 6));
    strlcpy(srRcpt.szHeaderLine, szGetRctHdr1(), TERM_ADDR_SIZE);
    //strcpy(srRcpt.szHeaderLine2, szGetRctFoot4());
	strcpy( srRcpt.szHeaderLine2, szGetRctHdr2());			//liem
    strcpy(srRcpt.szHeaderLine3, szGetRctHdr3());
    strcpy(srRcpt.szHeaderLine4, szGetRctHdr4());
    strcpy(srRcpt.szHeaderLine5, szGetRctHdr5());
    strlcpy(srRcpt.szHeaderLine6, szGetRctHdr6(), TERM_ADDR_SIZE);

    inMakeReceiptAmount(pobTran);

    memset(srRcpt.szClerkId, '\0', sizeof(srRcpt.szClerkId));

    if (inGetClerkMode() != NO_CLERK)
        strlcpy(srRcpt.szClerkId, pobTran->srBKey.szClerkId, CLERK_ID_SIZE);

    memset(srRcpt.szTransName, '\0', sizeof(srRcpt.szTransName));
    if (pobTran->srBRec.lnMessage)              /* mb1 */
        vdGetMessageFromReceiptFile(pobTran->srBRec.lnMessage, szTransName, pobTran);

    if (pobTran->srBKey.fVoided == VS_TRUE) {
        vdGetMessageFromReceiptFile(VOID_MSG6, srRcpt.szTransName, pobTran);
        pad(srRcpt.szTransName, srRcpt.szTransName, ' ', strlen(srRcpt.szTransName) + 1, LEFT);
    } else if (pobTran->srBRec.inNumberOfAdjusts > 0 || pobTran->srBRec.inNumberOfTipAdjusts > 0) {
        vdGetMessageFromReceiptFile(ADJUST_MSG6, srRcpt.szTransName, pobTran);

        //Trans Name too long for ColMode 0:12 & 1:16 Col when appended to "Adjust " PE457
        if (((inGetP350ColMode() == COLUMN_TYPE_24) || (inGetP350ColMode() == COLUMN_TYPE_32)) && (strlen(szTransName) > 10)) {
            srRcpt.szTransName[3] = 0x00; //So, cut "Adjust" to "Adj"
        }
        pad(srRcpt.szTransName, srRcpt.szTransName, ' ', strlen(srRcpt.szTransName) + 1, LEFT);
    }

    strcat(srRcpt.szTransName, szTransName);

    /* Pad Trans Name so that inverse print will have black symetrical */
    if (srPtrProps[inGetPrinterType()].inColumnType == COLUMN_TYPE_MULTIPLE)
        switch (inGetP350ColMode()) {
        case COLUMN_TYPE_24:
            pad(srRcpt.szTransName, srRcpt.szTransName, ' ', 12, CENTER);
            break;
        case COLUMN_TYPE_32:
            pad(srRcpt.szTransName, srRcpt.szTransName, ' ', 16, CENTER);
            break;
        case COLUMN_TYPE_42:
            pad(srRcpt.szTransName, srRcpt.szTransName, ' ', 21, CENTER);
            break;
        }
    else
        pad(srRcpt.szTransName, srRcpt.szTransName, ' ', (srPtrProps[inGetPrinterType()].inColumnType / 2), CENTER);

    strcpy(srRcpt.szHostName, szGetHostName());
    long2str(srRcpt.szBatchNum, lnGetBatchNum());
    pad(srRcpt.szBatchNum, srRcpt.szBatchNum, '0', inGetBatchNumSize(), RIGHT);

    strcpy(room, pobTran->srBRec.szRoomNum);
    if (strlen(room) > 0)
        strcpy(srRcpt.szInvNum, room);
    else if (pobTran->srBKey.fVoided || pobTran->fAdjust || pobTran->fTipAdjust || (pobTran->srBRec.fCloseTabSameCard))
        sprintf(srRcpt.szInvNum, SUBSTITUTE_LD, pobTran->srBKey.lnInvNum);
    else
        sprintf(srRcpt.szInvNum, SUBSTITUTE_LD, pobTran->srBKey.lnInvNum);


    sprintf(srRcpt.unFrm.srReceipt.szSTAN, SUBSTITUTE_LD, pobTran->srBKey.lnOrgSTAN);
    sprintf(srRcpt.unFrm.srReceipt.szECRInvNo, SUBSTITUTE_LD, pobTran->lnECRInvNum);


    memset(srRcpt.unFrm.srReceipt.szECRInvNo, 0, sizeof(srRcpt.unFrm.srReceipt.szECRInvNo));

    strcpy(srRcpt.unFrm.srReceipt.szServId, szGetSrvENum());

    if (strlen(pobTran->srBRec.szExpDate) > 0) {
        strcpy(szExpDate, pobTran->srBRec.szExpDate);
        sprintf(srRcpt.unFrm.srReceipt.szExpDate, "%.2s/%.2s", szExpDate + 2, szExpDate);
    }

    strcpy(srRcpt.unFrm.srReceipt.szCType, szGetCardLabel());

    if (inGetHDTTotRecs() > 1)
        strcpy(srRcpt.szHostName, szGetHostName());

    strcpy(srRcpt.unFrm.srReceipt.szCHolder, pobTran->srBRec.szCHolder);
    vdTrimSpaces(srRcpt.unFrm.srReceipt.szCHolder);

#ifdef SHIFT_DEFINED

    if (inGetShiftType() != NO_SHIFT)
        srRcpt.unFrm.srReceipt.chShift = pobTran->srBKey.chShift;
    else
        srRcpt.unFrm.srReceipt.chShift = '0';

#else
    srRcpt.unFrm.srReceipt.chShift = '0';

#endif

    return (VS_SUCCESS);
}


int inMakeReceiptAfterResp(TRANSACTION_OBJECT* pobTran) {
    short shTempSugTipPer1;
    short shTempSugTipPer2;
    short shTempSugTipPer3;
    long lnSChg = 0L;
    long lnBaseAmt;
    long lnSubTotAmt;

    /** /pdebug(("--vdMakeReceiptAfter--"));*/

    vdSetReceiptDateAndTime(pobTran);

    strcpy(srRcpt.unFrm.srReceipt.szApprovalCode, pobTran->srBRec.szAuthCode);

    memset(srRcpt.unFrm.srReceipt.szAcctNum, 0x00, sizeof(srRcpt.unFrm.srReceipt.szAcctNum));
    vdFormatPAN(pobTran, pobTran->srBRec.szPAN, srRcpt.unFrm.srReceipt.szAcctNum, sizeof(srRcpt.unFrm.srReceipt.szAcctNum));

    if (pobTran->srBRec.inChipStatus != NOT_USING_CHIP)
		strcpy(srRcpt.unFrm.srReceipt.szEntryMethod, "DIP"); // vdGetMessageFromFile(CHIP_ENTRY_MSG7, srRcpt.unFrm.srReceipt.szEntryMethod);
    else if (pobTran->srBRec.fManual)
        vdGetMessageFromFile(MANUAL_ENTRY_MSG, srRcpt.unFrm.srReceipt.szEntryMethod);
    else
        vdGetMessageFromFile(SWIPED_ENTRY_MSG, srRcpt.unFrm.srReceipt.szEntryMethod);

    if (pobTran->srBKey.fOffline)
        vdGetMessageFromFile(OFFLINE_MSG, srRcpt.unFrm.srReceipt.szApprovalType);
    else
        vdGetMessageFromFile(ONLINE_MSG, srRcpt.unFrm.srReceipt.szApprovalType);

    if (pobTran->srBRec.lnSChgAmount > 0L) {
        lnSChg = pobTran->srBRec.lnSChgAmount;

        memset(srRcpt.unFrm.srReceipt.szSChg, 0, sizeof(srRcpt.unFrm.srReceipt.szSChg));
        sprintf(srRcpt.unFrm.srReceipt.szSChg, SUBSTITUTE_LD, lnSChg);
        vdFormatAmount(srRcpt.unFrm.srReceipt.szSChg, VS_TRUE);

        lnSubTotAmt = (pobTran->srBKey.lnTotalOfTransactionAmount - lnSChg - pobTran->srBRec.lnCashBackAmount);
        sprintf(srRcpt.unFrm.srReceipt.szSubTotAmt, SUBSTITUTE_LD, lnSubTotAmt);
        vdFormatAmount(srRcpt.unFrm.srReceipt.szSubTotAmt, VS_TRUE);
    }

    sprintf(srRcpt.unFrm.srReceipt.szTotAmt, SUBSTITUTE_LD, (pobTran->srBKey.lnTotalOfTransactionAmount));
    vdFormatAmount(srRcpt.unFrm.srReceipt.szTotAmt, VS_TRUE);


    /** Calculations for suggested tips ****/


    if (fGetSugTipOn() == VS_TRUE)

    /* set default amounts if flag on and field is empty */ {
        shTempSugTipPer1 = inGetSugTipPer1();

        if (shTempSugTipPer1 <= 0) {
            shTempSugTipPer1 = 15;
            /* vdSetTCTSugTip1(shTempSugTipPer1);*/
        }

        sprintf(srRcpt.szTipPct1, SUBSTITUTE_D, shTempSugTipPer1);
        vdFormatPerCent(srRcpt.szTipPct1, WHOLE_PERCENT_FORMAT);

        shTempSugTipPer2 = inGetSugTipPer2();

        if (shTempSugTipPer2 <= 0) {
            shTempSugTipPer2 = 20;
            /*    vdSetTCTSugTip2(shTempSugTipPer2);*/
        }

        sprintf(srRcpt.szTipPct2, SUBSTITUTE_D, shTempSugTipPer2);
        vdFormatPerCent(srRcpt.szTipPct2, WHOLE_PERCENT_FORMAT);

        shTempSugTipPer3 = inGetSugTipPer3();

        if (shTempSugTipPer3 <= 0) {
            shTempSugTipPer3 = 25;
            /*         vdSetTCTSugTip3(shTempSugTipPer3);*/
        }


        sprintf(srRcpt.szTipPct3, SUBSTITUTE_D, shTempSugTipPer3);
        vdFormatPerCent(srRcpt.szTipPct3, WHOLE_PERCENT_FORMAT);

        inComputePercent((long) shTempSugTipPer1, srRcpt.szTipAmt1, pobTran->srBRec.lnBaseTransactionAmount, WHOLE_PERCENT_FORMAT);
        vdFormatAmount(srRcpt.szTipAmt1, VS_FALSE);
        vdTrimLeadSpaces(srRcpt.szTipAmt1);

        inComputePercent((long) shTempSugTipPer2, srRcpt.szTipAmt2, pobTran->srBRec.lnBaseTransactionAmount, WHOLE_PERCENT_FORMAT);
        vdFormatAmount(srRcpt.szTipAmt2, VS_FALSE);
        vdTrimLeadSpaces(srRcpt.szTipAmt2);

        inComputePercent((long) shTempSugTipPer3, srRcpt.szTipAmt3, pobTran->srBRec.lnBaseTransactionAmount, WHOLE_PERCENT_FORMAT);
        vdFormatAmount(srRcpt.szTipAmt3, VS_FALSE);
        vdTrimLeadSpaces(srRcpt.szTipAmt3);
    }


    sprintf(srRcpt.unFrm.srReceipt.szBaseAmt, SUBSTITUTE_LD, pobTran->srBRec.lnBaseTransactionAmount);
    if (pobTran->srBRec.inPrintOption & PRT_DUPLICATE) {
        lnBaseAmt = (pobTran->srBKey.lnTotalOfTransactionAmount - pobTran->srBRec.lnTaxAmount - pobTran->srBRec.lnTipAmount - pobTran->srBRec.lnCashBackAmount - lnSChg);
        sprintf(srRcpt.unFrm.srReceipt.szBaseAmt, SUBSTITUTE_LD, lnBaseAmt);
    } else {
        if (pobTran->srBKey.fVoided) {
            sprintf(srRcpt.unFrm.srReceipt.szTotAmt, SUBSTITUTE_LD, pobTran->srBKey.lnTotalOfTransactionAmount);
            vdFormatAmount(srRcpt.unFrm.srReceipt.szTotAmt, VS_TRUE);
            lnBaseAmt = (pobTran->srBKey.lnTotalOfTransactionAmount - pobTran->srBRec.lnTaxAmount - pobTran->srBRec.lnTipAmount - pobTran->srBRec.lnCashBackAmount - lnSChg);
            sprintf(srRcpt.unFrm.srReceipt.szBaseAmt, SUBSTITUTE_LD, lnBaseAmt);
        }
    }

    if (inGGCode() == PRE_AUTH)
        sprintf(srRcpt.unFrm.srReceipt.szBaseAmt, "%06.6ld", pobTran->srBRec.lnBaseTransactionAmount);
    else
        vdFormatAmount(srRcpt.unFrm.srReceipt.szBaseAmt, VS_TRUE);
    return(VS_SUCCESS);
}



/*-------------------------------------------------------------------------
   Function :   vdNotPrintCloseTab
    Description :
    Parameters :
    Returns :
    Globals :
    Notes :
    Modification History :
      #     Date      Who                     Description
    ----- -------- ---------- ---------------------------------------------
      1   11/22/96 Page_K1    Add BATCH_REVIEW_OPERATION condition
      2   02/19/98 Page_K1    Use pobTran->srBKey.fOffline instead of
                               fGetTRTOffline().
--------------------------------------------------------------------------*/

VS_BOOL fPrintCloseTab(TRANSACTION_OBJECT* pobTran) {
    if (fGetCloseTabOptRec() == VS_TRUE)
        return(VS_TRUE);
    if (inGGOpCode() == REPRINT_ANY_OPERATION)
        return(VS_TRUE);
    if (inGGOpCode() == BATCH_REVIEW_OPERATION)
        return(VS_TRUE);
    if (pobTran->srBRec.fCloseTabNewCard/*!pobTran->srBKey.fOffline*/)
        return(VS_TRUE);

    return(VS_FALSE);
}


int inPrintReceiptTop(TRANSACTION_OBJECT* pobTran) {
    /** /pdebug(("--inPrintReceiptTop--"));*/
    if (pobTran->fPreprinted)
        return(VS_SUCCESS);

    if (inGetPrinterType() == NOPRINTER && fGetEnablePrinter()) {
        //      vdDisplayMessageFromFile( NO_PRINTER_MSG, WARNING_MSG);
        return(VS_SUCCESS);
    }

    if (!fGetEnablePrinter())
        return(VS_SUCCESS);

    if (!fGetPrintReceipt(pobTran) && !(pobTran->srBRec.inPrintOption & PRT_DUPLICATE))
        return(VS_SUCCESS);

    if (((inGGCode() == PRE_COMP) && (!fPrintCloseTab(pobTran))) || ((inGGCode() == PRE_AUTH) && (fGetOpenTabOptRec() == VS_FALSE))) {
        vdDisplayMessageFromFile(NO_RCPT_ERR_MSG, WARNING_MSG);
        pause(50);
        return(VS_SUCCESS);
    }

    vdDisplayMessageFromFile(PRINTING_MSG, STATUS_MSG);

    /* preprint up to account number */

    if (inPrintLines(&obPrinter, HEADER_START, TOP_END, NO_BL_LINE, lnHeaderCondition) != VS_SUCCESS)
        return(VS_ERR);
    pobTran->fPreprinted = VS_TRUE;


    return(VS_SUCCESS);
}




int inPrintReceiptMiddle(TRANSACTION_OBJECT* pobTran) {
    /** /pdebug(("--inPrintReceiptBottom--"));*/

    if (inGetPrinterType() == NOPRINTER || !fGetEnablePrinter())
        return(VS_SUCCESS);

    if (!fGetPrintReceipt(pobTran) && !(pobTran->srBRec.inPrintOption & PRT_DUPLICATE))
        return(VS_SUCCESS);

    if ((inGGCode() == PRE_COMP) && (!fPrintCloseTab(pobTran)))
        return(VS_SUCCESS);

    if ((inGGCode() == PRE_AUTH) && (fGetOpenTabOptRec() == VS_FALSE))
        return(VS_SUCCESS);

    vdDisplayMessageFromFile(PRINTING_MSG, STATUS_MSG);

    if (inPrintLines(&obPrinter, MIDDLE_START, MIDDLE_END, NO_BL_LINE, lnMiddleCondition) != VS_SUCCESS)
        return(VS_ERR);
    return(VS_SUCCESS);
}



int inPrintReceiptBottom(TRANSACTION_OBJECT* pobTran) {
    DB_FILE dbReprintFile;

    /**/pdebug(("--inPrintReceiptBottom--"));/**/

    if (inGetPrinterType() == NOPRINTER || !fGetEnablePrinter())
        return(VS_SUCCESS);


    if (!fGetPrintReceipt(pobTran) && !(pobTran->srBRec.inPrintOption & PRT_DUPLICATE))
        return(VS_SUCCESS);

    if ((inGGCode() == PRE_COMP) && (!fPrintCloseTab(pobTran)))
        return(VS_SUCCESS);

    if ((inGGCode() == PRE_AUTH) && (fGetOpenTabOptRec() == VS_FALSE))
        return(VS_SUCCESS);

    vdDisplayMessageFromFile(PRINTING_MSG, STATUS_MSG);

    if (inPrintLines(&obPrinter, BOTTOM_START, FOOTER_END, NO_BL_LINE, lnBottomCondition) != VS_SUCCESS)
        return(VS_ERR);

    if (db_open(&dbReprintFile, REPRINT_FILE_NAME, SIZE_BATCH_KEY, (O_TRUNC | O_CREAT | O_RDWR)) < 0)
        return(VS_ERR);

    if (inWriteBatchData(&dbReprintFile, &pobTran->srBKey, &pobTran->srBRec, DB_APPEND) != VS_SUCCESS) {
        inCloseBatch(&dbReprintFile);

        vdSGErrorMessage(BATCH_ERR_MSG);
        return(VS_ERR);
    }

    if (inCloseBatch(&dbReprintFile) != VS_SUCCESS)
    {

        vdSGErrorMessage(BATCH_ERR_MSG);
	}
    pobTran->fPreprinted = VS_FALSE;

    return(VS_SUCCESS);
}


/*********************************************************************************************
* @func int | inPrintErrorMsg |
*       Form flow function to print error msg
*
* @parm TRANSACTION_OBJECT *| pobTran
*       Transaction object
*
* @rdesc return values.
* @flag int    |  VS_ERR
* @flag int    |  VS_SUCCESS
* @end
**********************************************************************************************/

int inPrintErrorMsg(TRANSACTION_OBJECT* pobTran) {
    if (inGetPrinterType() == NOPRINTER || !fGetEnablePrinter())
        return(VS_SUCCESS);

    if (pobTran->srBRec.inPrintOption == PRT_ERROR) {
        /*
         * if englthin.tpl, pad the error message so that reverse printing
         * prints all the way across the receipt.
         */
        if ((srPtrProps[inGetPrinterType()].inColumnType == COLUMN_TYPE_MULTIPLE && inGetP350ColMode() == COLUMN_TYPE_32) || (srPtrProps[inGetPrinterType()].inColumnType == COLUMN_TYPE_32))
            pad(srRcpt.szErrorMessage, srRcpt.szErrorMessage, ' ', 32, CENTER);

        /* added code to check for 42 column mode - this allows reverse printing for error
           messages to print all the way across the receipt. gw */

        else if ((srPtrProps[inGetPrinterType()].inColumnType == COLUMN_TYPE_MULTIPLE && inGetP350ColMode() == COLUMN_TYPE_42) || (srPtrProps[inGetPrinterType()].inColumnType == COLUMN_TYPE_42))
            pad(srRcpt.szErrorMessage, srRcpt.szErrorMessage, ' ', 42, CENTER);

        /* added code to check for 24 column mode - this allows reverse printing for error
           messages to print all the way across the receipt.  */

        else if ((srPtrProps[inGetPrinterType()].inColumnType == COLUMN_TYPE_MULTIPLE && inGetP350ColMode() == COLUMN_TYPE_24) || (srPtrProps[inGetPrinterType()].inColumnType == COLUMN_TYPE_24))
            pad(srRcpt.szErrorMessage, srRcpt.szErrorMessage, ' ', 24, CENTER);

        lnBottomCondition = 0x20000000;

        if (inPrintLines(&obPrinter, FOOTER_START, FOOTER_END, NO_BL_LINE, lnBottomCondition) != VS_SUCCESS)
            return(VS_ERR);
    }

    return(VS_SUCCESS);
}


/*-------------------------------------------------------------------------

    This function is currently being used by inPrintHostCaptureReport () in report2.c
--------------------------------------------------------------------------*/

void vdSetPrintHeaderCondition(TRANSACTION_OBJECT* pobTran,VS_BOOL fAllowRedPrint,VS_BOOL fUnused) {
    VS_BOOL fRedLine = VS_FALSE;

    if (inGetPrinterType() == NOPRINTER || !fGetEnablePrinter())
        return;


    lnHeaderCondition = formater_set_flags(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, (strlen(srRcpt.szHeaderLine6) > 0),
                        0,
                        (strlen(srRcpt.szHeaderLine5) > 0),

                        0,
                        (strlen(srRcpt.szHeaderLine4) > 0),
                        0,
                        (strlen(srRcpt.szHeaderLine3) > 0),

                        0,
                        (strlen(srRcpt.szHeaderLine2) > 0),
                        0,
                        (strlen(srRcpt.szHeaderLine) > 0), -1);
}

/*-------------------------------------------------------------------------
    Function :    inBuildReceipt
    Description :
    Parameters : Receipt Type(original, duplicate)
    Returns :
    Notes :
-------------------------------------------------------------------------*/

void vdBuildReceipt(TRANSACTION_OBJECT* pobTran) {
    inMakeReceiptTop(pobTran);
    vdSetPrintHeaderCondition(pobTran, VS_FALSE, VS_FALSE);
}

/*-------------------------------------------------------------------------
This function is being used by reports.
--------------------------------------------------------------------------*/

int inPrintReceiptHeader(TRANSACTION_OBJECT* pobTran) {

    memset(srRcpt.szHeaderLine, 0, (sizeof(srRcpt.szHeaderLine) * 6));

    strlcpy(srRcpt.szHeaderLine, szGetRctHdr1(), TERM_ADDR_SIZE);

    //strcpy(srRcpt.szHeaderLine2, szGetRctFoot4());
	strcpy( srRcpt.szHeaderLine2, szGetRctHdr2() );			//liem

    strcpy(srRcpt.szHeaderLine3, szGetRctHdr3());

    strcpy(srRcpt.szHeaderLine4, szGetRctHdr4());

    strcpy(srRcpt.szHeaderLine5, szGetRctHdr5());

    strlcpy(srRcpt.szHeaderLine6, szGetRctHdr6(), TERM_ADDR_SIZE);

    vdSetReportHeaderCondition(pobTran);
    // HEADER_START has be change to 2 , but it has to be moved back to 1
    // HEADER START has been changed to 6 from 10


    if (inPrintLines(&obPrinter, HEADER_START, HEADER_END, NO_BL_LINE, lnHeaderCondition) != VS_SUCCESS)
        return(VS_ERR);

    pobTran->fPreprinted = VS_TRUE; /* bb1 */

    return(VS_SUCCESS);
}

#undef VSRETFRM_C


